<?php
namespace app\admin\controller;

//use app\admin\model\ProductAttr;
//use app\admin\model\ProductMenu;
//use app\admin\model\ProductSku;
//use app\admin\model\ProductSkuAttr;
use think\facade\View;
use think\Request;

class GoodsCategory extends Base
{
    protected $ModelPath = 'GoodsCategory';
    protected $model = null;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->con = "GoodsCategory";
        $this->title = "商品类别";
//        $this->pageSize = 15;
        $module = $this->ModelPath;
        $this->model = new \app\admin\model\GoodsCategory();

        View::assign(array(
            'module' => $module,
            'title' => $this->title,
            'con' => $this->con,
            'pageSize' => $this->pageSize
        ));
    }

    public function index()
    {
        if(request()->isPost()){
            $params = request()->param();
            $map = [];
            $map[] = ['id', '>', 0];

//            $this->model = $this->model->where('merchant_id', $this->merchantId);
//            if(isset($params['title'])){//return 1;
//                // $title = input('title');
//                $this->model = $this->model->where("title", "like", $params['title']);
//            }
//            if(isset($params['author_id'])){
//                // $authorId = input('author_id');var_dump(1);die;
//                $this->model = $this->model->where("author", "like", $params['author_id']);
//            }

//             if(input('tag_id')){
//                 $authorId = input('tag_id');
// //                $map['_string'] = 'FIND_IN_SET(' . $authorId . ', tag_list)';
//                 $map[] = ['exp', Db::raw("FIND_IN_SET(" . $authorId . ", tag_list)")];
//             }

            $total = $this->model
                 ->where($map)
                ->count();

            $pageIndex = max(input('page'), 1);
            $pageSize = $this->pageSize;
            $p = ($pageIndex - 1) * $pageSize;

            $list = $this->model
                 ->where($map)
                // ->where('rec',1)

                ->order('id DESC')
//                ->append(['cover_format', 'nav_format', 'unit_format'])
                ->limit($p, $pageSize)
                ->select();
// var_dump($list);die;
            $arr['data'] = $list;
            $arr['code'] = 0;
            // var_dump(1);die;
            $arr['count'] = $total;
            $arr['limit'] = $pageSize;
            $arr['msg'] = "获取" . $this->title . "列表成功";
            return json($arr);
        }else{
            return View::fetch();
        }
    }

    public function add(){
        if(request()->isPost()){
            $params = request()->param();
//            dd($params);
//
//            $code = input('code');
//            $codeArr = explode(',', $code);
//            $sum = $codeArr[0] + $codeArr[1] + $codeArr[2] + $codeArr[3];
//            $num = input('num');
            $add = [
                'market_amount'=>bcmul_amount($params['market_amount']),
                'sale_amount'=>bcmul_amount($params['sale_amount']),
                'sale_amount1'=>bcmul_amount($params['sale_amount1']),
                'sale_amount2'=>bcmul_amount($params['sale_amount2']),
                'sale_amount3'=>bcmul_amount($params['sale_amount3']),
                'sale_amount4'=>bcmul_amount($params['sale_amount4']),
                'sale_amount5'=>bcmul_amount($params['sale_amount5']),
                'name'=>$params['name'],
                'images'         =>  json_encode([]),
                'describe'     =>  '11',
                'merchants_id'     =>  1,
                'merchant_id'     =>  1,//$this->merchantId,
                'cover'     =>  input('cover'),
//                'sum'     =>  $sum,
//                'status'     =>  1,
            ];

//            var_dump($add);die;

            $this->model->data($add);
            $result = $this->model->save();
            if ($result !== false) {
//                $productSku = \app\merchant\model\ProductSku::create([
//                    'good_id'=>$this->model->id,
//                    'image'=>input('cover'),
//                    'price'=>input('price'),
//                    'stock'=>input('stock'),
//                    'origin_price'=>input('origin_price'),
//                    'cost_amount'=>input('cost_amount'),
//                ]);
//                $productAttrParent = ProductAttr::create([
//                    'good_id'=>$this->model->id,
//                    'pid'=>0,
//                    'name'=>'规格',
//                    'weigh'=>'piece',
//                ]);
//                $productAttr = ProductAttr::create([
//                    'good_id'=>$this->model->id,
//                    'pid'=>$productAttrParent->id,
//                    'name'=>input('attr'),
//                    'weigh'=>'piece',
//                ]);
//                ProductSkuAttr::create([
//                    'sku_id'=>$productAttr->id,
//                    'attr_id'=>$productSku->id,
//                ]);
                $arr['status'] = 1;
                $arr['msg'] = '添加成功';
            } else {
                $arr['status'] = 0;
                $arr['msg'] = '添加失败';
            }
            return $arr;
        }else{
//            $numArr = Db::name('num')
//                ->where('type', 1)
//                ->order('id DESC')->select();
//            View::assign(array(
//                'numArr'        =>    $numArr,
//            ));
            return View::fetch();
        }
    }

    public function edit(){
        if(request()->isPost()){
            $params = request()->param();
            unset($params['file']);

//            $update = $params;//dd($update);

            $update = [
                'id'=>$params['id'],
                'market_amount'=>bcmul_amount($params['market_amount']),
                'sale_amount'=>bcmul_amount($params['sale_amount']),
                'sale_amount1'=>bcmul_amount($params['sale_amount1']),
                'sale_amount2'=>bcmul_amount($params['sale_amount2']),
                'sale_amount3'=>bcmul_amount($params['sale_amount3']),
                'sale_amount4'=>bcmul_amount($params['sale_amount4']),
                'sale_amount5'=>bcmul_amount($params['sale_amount5']),
                'name'=>$params['name'],
//                'images'         =>  json_encode([]),
//                'describe'     =>  '11',
//                'merchants_id'     =>  1,
//                'merchant_id'     =>  1,//$this->merchantId,
//                'cover'     =>  input('cover'),
//                'sum'     =>  $sum,
//                'status'     =>  1,
            ];

//            $update['original_price'] = $update['original_price'] * 100;
//            $update['sales_price'] = $update['sales_price'] * 100;
//            $update['cost_price'] = $update['cost_price'] * 100;
            if(!empty($params['images'])){
                $update['images'] = json_encode(json_decode(input('images'), true));
            }else{
                unset($update['images']);
            }
            if(!empty($params['content'])){
//                $update['content'] = implode(',', json_decode(input('content'), true));//json_encode(json_decode(input('content'), true));
                $update['content'] = json_encode(json_decode(input('content'), true));
            }else{
                unset($update['content']);
            }

            $save = $this->model->update($update);
            if ($save) {
                $arr['status'] = 1;
                $arr['msg'] = '修改成功';
            } else {
                $arr['status'] = 0;
                $arr['msg'] = '修改失败';
            }
            return $arr;
        }else{
            $id = input('id');
            $info = \app\admin\model\Goods::where('id', $id)
                ->append(['cover_format', 'images_format', 'images_parse', 'content_format', 'content_parse',
                    'market_amount_format', 'sale_amount_format', 'sale_amount1_format', 'sale_amount2_format',
                    'sale_amount3_format', 'sale_amount4_format', 'sale_amount5_format'])
                ->find();
            //dd($info['content_format']);
            View::assign(array(
                'id'          => $id,
                'info'        =>    $info,
                'images_parse'=>json_encode($info['images_parse']),
                'content_parse'=>json_encode($info['content_parse']),
            ));
//            dd($info);
            return View::fetch();
        }
    }

    public function isShowStatus()
    {
        $id = request()->param('id');
        $status = request()->param('status');
        $value = $status == 'true' ? 'on' : 'off';
        $save = $this->model->where('id', $id)->update([
            'is_show'=>$value
        ]);
        if ($save) {
            $arr['status'] = 1;
            $arr['msg'] = '成功';
        } else {
            $arr['status'] = 0;
            $arr['msg'] = '失败';
        }
        return $arr;
    }
}